package version1;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
//TODO http://stackoverflow.com/questions/9697546/java-how-to-insert-rowsdata-into-jtable-explicitly-if-rows-are-inserted-by-ab/9697983#9697983
public class CoreTable extends AbstractTableModel implements Runnable{
	
	
	private List<String> columnNames = new ArrayList();
    private List<List> data = new ArrayList();
	private List<String> standardList = new ArrayList();
    private CondorHandler handler = new CondorHandler();
    private String CORE = "CORE";
    private String JOB = "JOB"; 
	private int sleepTime;
	String typeOfJob;
	private static int CORESLEEPTIME = 80000;
	private static int JOBSLEEPTIME = 10000;
    
    public CoreTable(String typeOfJob) {
//TODO put some parameters in CoreTable(), depending which table to invoke, core or job
//TODO call Condor to get standardtabledata
    	this.typeOfJob = typeOfJob;
	    if(CORE == typeOfJob){
	    	standardList = handler.getCoreFilteringList();
	    	 data.add(handler.getCoreFilteringList());
	    	 sleepTime = CORESLEEPTIME;
	    }
	    else if(JOB == typeOfJob){
	    	standardList = handler.getJobFilteringList();
	    	data.add(handler.getJobFilteringList());
	    	sleepTime = JOBSLEEPTIME;
	    }	
	
	    columnNames.addAll(standardList);
    }
    
	public int getColumnCount() {
		return columnNames.size();
		
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		return columnNames.get(col);
	}
	public List<String> getAllColumnNames(){
		return columnNames;
	}

	public Object getValueAt(int row, int col) {
		return data.get(row).get(col);
	}

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}
	
   public void addRow(List rowData){
	   data.add(rowData);
	   fireTableRowsInserted(data.size() - 1, data.size() - 1);
   }
   public void deleteColumn(String row){
	   data.remove(row);
   }
   public void createNewTable(List<String> table){
//TODO call Condor to get  tabledata depending on arraylist parameter -> data.addAll(condorTable)
	   columnNames.clear();
	   		columnNames.addAll(table);
	   		
		   fireTableStructureChanged();
		 
   }
   public void createNewList(List<String> table){
	   data.clear();
	   data.add(table);
	   fireTableRowsInserted(data.size() - 1, data.size() - 1);
   }
   
   public void testAddMethod(){
	    data.add(columnNames);
	    fireTableDataChanged();
   }

@Override
public void run() {
	while(true){
//			System.out.println("call to Condor: "+sleepTime+typeOfJob);
//	        createNewList(handler.getJobFilteringList());
			createNewList(columnNames);
			testAddMethod();
			try {
	    		Thread.sleep(sleepTime);
	    	} catch (InterruptedException e) {}
	    }
	}

}

